/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package com.hp.bon.sgw.scsp;

import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.hp.bon.sgw.scsp.SCSPConstants.ResultCode;

/**
 * (<strong>Entry Point</strong>) Starts SumUp client.
 * 
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 */
public class LoopBackClient extends SessionHandler {
	private static final String HOSTNAME = "localhost";

	private static final int PORT = 6080;

	private static final long CONNECT_TIMEOUT = 300 * 1000L; // 30 seconds

	public static void main(String[] args) {

		int port = 6081;
		String hostId = "scspNE1@001.ctnbc-bon.net";
		String hostAddress = "127.0.0.1";
		String serverHostId = "SCSPServer@001.ctnbc-bon.net";
		SessionHandler sessionHandler = new SessionHandler();
		sessionHandler.setHostConf(hostId, hostAddress, port, "", "");
		sessionHandler.addPersistentPeer(new Peer(serverHostId, hostAddress,
				PORT));
		sessionHandler.setConnectionTimeout(3, 300);
		sessionHandler.startAsClient();
		LOGGER.info("LoopBackClient start as Client");

		Element rootSCR = DocumentHelper
				.createElement(SessionHandler.MSG_TYPE_SCR);
		rootSCR.addAttribute(SessionHandler.MSG_FIEDD_SCID,
				"PaymentRec.Query@010.ChinaTeleCom.com");
		rootSCR.addAttribute(SessionHandler.MSG_FIEDD_UID, "name");
		// String pwd = null;
		// try {
		// pwd = RSAUtil
		// .getReadableMD5Data("986F8C8D0722A961E297553DE9CE82E4"
		// .getBytes("iso-8859-1"));
		// } catch (UnsupportedEncodingException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// rootSCR.addAttribute(SessionHandler.MSG_FIEDD_PWD,pwd);
		rootSCR.addAttribute(SessionHandler.MSG_FIEDD_RTT, "2011111001001010");
		
		String dataStr = "<Service_Information><Service_Result_Code>11</Service_Result_Code></Service_Information>";
//		DATA Convert begin	
//		SCSPUtils.addElement(rootSCR,MSG_NODE_DATA, dataStr);
//		DATA no Convert begin				
		try {
			SCSPUtils.addElement(rootSCR,MSG_NODE_DATA,
					DocumentHelper.parseText(dataStr).getRootElement());
		} catch (DocumentException e1) {
			System.out.println(e1);
		}
		
		rootSCR.addAttribute(SessionHandler.MSG_FIEDD_MID,
				SCSPUtils.makeSessionID(hostId));
		System.out.println("Begin to send Msg");
		int rc = ResultCode.ERR_SESSION_NOT_EXIST;
		while (rc == ResultCode.ERR_SESSION_NOT_EXIST) {
			rc = sessionHandler.sendRequest(serverHostId, rootSCR,null);
			SCSPUtils.sleep(5000);
		}

	}
}
